package patrol.mobile.com.mobilepatrol.fragment;


import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import patrol.mobile.com.mobilepatrol.R;

/**
 * 消息
 */
public class MessageFragment extends BaseFragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.tab_layout_message_activity)
    TabLayout tabLayoutMessageActivity;
    @BindView(R.id.vp_message_activity)
    ViewPager vpMessageActivity;

    private String mParam1;
    private String mParam2;


    public MessageFragment() {
        // Required empty public constructor
    }

    public static MessageFragment newInstance(String param1, String param2) {
        MessageFragment fragment = new MessageFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_message;
    }

    @Override
    protected void initTitle() {
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText("消息");
    }

    @Override
    protected void initData() {
        TabFragmentAdapter adapter = new TabFragmentAdapter(getChildFragmentManager(), getActivity());
        vpMessageActivity.setAdapter(adapter);
        tabLayoutMessageActivity.setupWithViewPager(vpMessageActivity);
        switchTab(0);
    }


    private void switchTab(int index) {
        if (vpMessageActivity != null) {
            vpMessageActivity.setCurrentItem(index, false);
        }
    }

    @Override
    protected void initListener() {

    }

    /*
     * 此结构为activity+fragment+fragment
     */
    static class TabFragmentAdapter extends FragmentStatePagerAdapter {
        private String[] TITLES;

        private Fragment[] tabs;

        private TabFragmentAdapter(FragmentManager fm, Activity context) {
            super(fm);
            TITLES = context.getResources().getStringArray(R.array.message_activity_titles);
            tabs = new Fragment[TITLES.length];
            tabs[0] = MessageSubFragment.newInstance("未阅","");//未阅读
            tabs[1] = MessageSubFragment.newInstance("已阅","");//已阅读
        }

        @Override
        public Fragment getItem(int position) {
            return tabs[position];
        }

        @Override
        public int getCount() {
            return TITLES.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return TITLES[position];
        }
    }
}
